<?php
# header of each page
function head($title='', $meta_description='', $meta_keywords='', $active='') {
header("Content-type: text/html; charset=UTF-8");
?>
<!DOCTYPE html>
<html>
<head>
<title><?=$title?></title>
<meta charset="utf-8">
<base href="<?=HTTPS_BASE?>">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="<?=$meta_description?>">
<link rel="stylesheet" href="<?=HTTPS_BASE?>css/main.css?v=1">
<link rel="stylesheet" href="<?=HTTPS_BASE?>css/style.css">

<script type="text/javascript" >

  	<?php 
	$signed_request = $_REQUEST['signed_request']; // Get the POST signed_request variable.
	
	if(isset($signed_request)) // Determine if signed_request is blank.
	 {
	 $pre = explode('.',$signed_request); // Get the part of the signed_request we need.
	 $json = base64_decode($pre['1']); // Base64 Decode signed_request making it JSON.
	 $obj = json_decode($json,true); // Split the JSON into arrays.
	
	 ?>
	  window.location.href = '<?=HTTPS_BASE?><?=base64_decode($obj['app_data'])?>';
	  <?php
	}
 
?>	
 // window.parent.history.pushState("object or string", "Title", "?app_data=<?=$_GET['prm1']?>");
  
</script>

</head>
<body>

    <div id="wrapper">
        <header id="header">
            <nav>
            <?php
			getMCategory('parent',0)
			?>
            </nav>
        </header> <!--#header-->
 	
	
<?php	
}


function getMCategory($child,$parent_id){
	
	 $sql_c = "SELECT
                    c.category_id,
                    d.Name,
					s.sef_url
                    
                FROM
                    category AS c,
                    category_description AS d,
					sef_url AS s
                WHERE
                
                    c.category_id = d.category_id
                    
                    AND
					
					c.category_id = s.query
                    
                    AND
                    
                    d.lang_id = '1'
					
					AND
                    
                    s.lang_id = '1'
					
					AND
                    
                    s.tamplate = 'category'
                    
                    AND
                    c.published
					
					AND parent_id = '".$parent_id."'
                    
                    ORDER BY c.order_number DESC
                    
                  ";
                
                    $result_c = query($sql_c);
                    
					if( $result_c){
					?>
                    
                    <ul class="<?=$child?>">
                    
                    
                    <?php
					
					if($child == 'parent'){
					/*?>
						<li>
                        	<a href="<?=HTTPS_BASE?>home">Начало</a>
                   		</li>
                    <?php	*/
					}
                    while ($row = mysql_fetch_object($result_c)) {
                          ?>
                        <li>
                        <a href="<?=HTTPS_BASE.$row->sef_url?>"><?=sth($row->Name)?></a>
        			<?php $_SESSION['i']++;
						if($child == 'parent') $child_new = 'child';
						if($child == 'child') $child_new = 'grand-child';
						getMCategory($child_new,$row->category_id);
						 ?>
                        </li>
        				<?php
					}
					if($child == 'parent'){
					?>
						<li>
                        	<a href="<?=HTTPS_BASE?>map">Магазини</a>
                   		</li>
                    <?php	
					}
					?>
                    </ul>
                    <?php
					}
					
}


# footer of each page
function foot($tmp='') {
	
?>
  </div> <!--#wrapper-->

    <!--vendor js-->
    
    <!--app js-->
    <script src="<?=HTTPS_BASE?>js/main.js"></script>
    <div id="fb-root"></div>
	<script type="text/javascript" src="https://connect.facebook.net/bg_BG/all.js"></script>
   
    
    <script type="text/javascript">
		FB.init({
		 appId  : '812280832170775',
		 status : true, // check login status
		 cookie : true, // enable cookies to allow the server to access the session
		 xfbml  : true  // parse XFBML
		});
		
		FB.Canvas.setDoneLoading( function(response) {
			console.log(response.time_delta_ms);
			FB.Canvas.setAutoGrow();
			FB.Canvas.scrollTo(0,370);
		});

	  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
	  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
	  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
	  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
	
	  ga('create', 'UA-56841035-1', 'auto');
	  ga('send', 'pageview');

    </script> 
</body>
</html>
		

<?php
}

# a redirecting function that adds the session ID if cookies are disabled and stops the execution of the script
function redirect($location) {
	if (!$location) error("Missing arguments.");

	$a = parse_url($location);
	if (!isset($a['query'])) {
		$a['query'] = '';
	}
	
	$session_delimiter = ($a['query'])?'&':'?';
	header("Location: $location".addsession($session_delimiter));
	exit;
}

# adding the session_id when cookies are disabled
function addsession($symbol="?") {
	return (!isset($_COOKIE[session_name()])&&session_id())?$symbol.session_name()."=".session_id():"";
}

function escape($string,$encoding=false) {
  return mysql_real_escape_string( (($encoding)?encoding($string):$string) );
}

function encoding($string) {
  return iconv('ISO-8859-1', 'UTF-8', $string);
}

function sth($string) {
  return htmlspecialchars(stripslashes($string));
}

# checks whether an email is valid or not
function checkEmail($email) {
	return preg_match("/^[A-z0-9\.\-]+@(?:[A-z0-9\-]+\.)+[A-z0-9]{1,6}$/", $email);
}

# Cut a text to specified chracters length
function text_format($text, $length) {
	$str = '';
	$text_length = mb_strlen($text, "UTF-8");
	 
	if ($text_length < $length) {
		$str = $text;		
	} else {
		$position_end = strpos($text, ' ', $length);
		$str = substr($text, 0, ($position_end)?$position_end:$text_length);
	}
	
	return $str;
}

function text_format_blog($text, $length) {
	$str = '';
	$text_length = mb_strlen($text, "UTF-8");
	 
	if ($text_length < $length) {
		$str = $text;		
	} else {
		$position_end = strpos($text, '<hr />', $length);
		$str = substr($text, 0, ($position_end)?$position_end:$text_length);
	}
	
	return $str;
}


# general error function
function error($s="An error occured.") {
  if (!headers_sent()) {
    head("Error");
    echo "<table cellpadding=4 width=".SITE_WIDTH." align=center border=0><tr><td align=center class=title>$s</td></tr></table>";
    foot();
  }
  else
    echo $s;
  exit;
}

# debug function for dumping a variable value
function pp($var, $stop=false) {
  echo "<pre>";
  print_r($var);
  echo "</pre>";
  if ($stop) exit;
}

# Generate numbers
function generateNumber () {

  # start with a blank number
  $number = "";

  # define possible characters
  $possible = "0123456789"; 

  # pick a random character from the possible ones
  $number = substr($possible, mt_rand(0, strlen($possible)-1), 1);

  # done!
  return $number;

}



function json_c_decode($json, $assoc = true, $depth = 512, $options = 0) {
			
				$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);
				
				if(version_compare(phpversion(), '5.4.0', '>=')) {
					$json = json_decode($json, $assoc, $depth, $options);
				}
				elseif(version_compare(phpversion(), '5.3.0', '>=')) {
					$json = json_decode($json, $assoc, $depth);
				}
				else {
					$json = json_decode($json, $assoc);
				}
			
				return $json;
		}



?>